/**
 *
 */
package com.moogene.saad.game;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.widget.Toast;

import java.util.HashMap;
import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author 28850924
 *
 */
public final class TheGame {
    static private final HashMap<Context, TheGame> mMap = new HashMap<Context, TheGame>();
    public static String APP_TAG = "SAADGAME:";

    static public TheGame get(Context context) {
        return mMap.get(context);
    }

    public static final TimeZone CURRENT_TIME_ZONE = TimeZone.getDefault();

    private final Context mContext;

    static private final HandlerThread mHandlerThread = new HandlerThread("AppHandlerThread");

    static private Handler mHandler = null;

    private boolean mPaused = false;

    /**
     * If true, the Gallery activity will not start the cache service when the
     * activity is stopped.
     */
    public AtomicBoolean preventCacheService = new AtomicBoolean(false);

    public TheGame(Activity actvitiy) {
        // register
        mMap.put(actvitiy, this);

        mContext = actvitiy;

        if (mHandler == null) {
            mHandlerThread.start();
            mHandler = new Handler(mHandlerThread.getLooper());
        }
    }

    public void shutdown() {


        // unregister
        mMap.remove(mContext);
    }

    public Context getContext() {
        return mContext;
    }

    public Handler getHandler() {
        while (mHandler == null) {
            // Wait till the handler is created.
            ;
        }
        return mHandler;
    }

    public boolean isPaused() {
        return mPaused;
    }

    public void onResume() {
        mPaused = false;
    }

    public void onPause() {
        mPaused = true;
    }


    public static final String getCachePath(final String subFolderName) {
        return Environment.getExternalStorageDirectory()
                + "/Android/data/com.moogene.android.saad/cache/" + subFolderName;
    }

    public void showToast(final String string, final int duration) {
        mHandler.post(new Runnable() {
            public void run() {
                Toast.makeText(mContext, string, duration).show();
            }
        });
    }
}
